package learn.basics.polymorphic.demo2;

abstract class Animal{
    public abstract void Eat();
}

class Cat extends Animal{

    @Override
    public void Eat() {
        System.out.println("猫吃鱼");
    }

    public void CatchMouse(){
        System.out.println("猫抓老鼠");
    }
}
class Dog extends Animal{
    @Override
    public void Eat() {
        System.out.println("狗吃肉");
    }

    public void WatchHouse(){
        System.out.println("狗看家");
    }
}

class Demo01{
    public static void Test(){
        Animal animal=new Cat();
        animal.Eat();

        //向下转型
        Cat cat=(Cat) animal;
        cat.CatchMouse();
    }
}

class Demo2Instanceof{

    public static void Test(){
        Animal animal=new Dog();
        animal.Eat();

        if(animal instanceof Dog){
            Dog dog=(Dog) animal;
            dog.WatchHouse();
        }

        if(animal instanceof Cat){
            Cat cat=(Cat) animal;
            cat.CatchMouse();
        }
    }
}





public class TestPolymorphic{
    public static void main(String[] args) {
        Demo01.Test();
    }
}
